﻿using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Linq;
using System.Numerics;
using System.Text;
using System.Threading.Tasks;

namespace LinearFunctionFitting.Demo.Algorithm
{
    public class OrdinaryMethods : IFunction
    {
        public (int a, int b) LinearFunction(int[] xs, int[] ys)
        {
            int n = xs.Length;
            int a = 0;
            int b = 0;
            int sumX = 0;
            int sumY = 0;
            int sumXY = 0;
            int sumX2 = 0;
            int x2Sum = 0;
            for (int i = 0; i < n; i++)
            {
                int x= xs[i];
                int y= ys[i];
                sumX += x;
                sumY += y;
                sumXY += x * y;
                sumX2 += x * x;
            }
            x2Sum = sumX * sumX;
            int nsumX2 = n * sumX2;
            int k = x2Sum - nsumX2;
            b = (sumX * sumXY - sumY * sumX2) / k;
            a = (sumY * sumX - n * sumXY) / k;
            return (a, b);
        }
    }
}
